package com.how2java.springboot.web;

import com.how2java.springboot.po.BiUserApprovalChangePO;
import com.how2java.springboot.po.SystemUserPO;
import com.how2java.springboot.request.OAUserCallbackRequest;
import com.how2java.springboot.service.BiUserApprovalChangeService;
import com.how2java.springboot.service.SystemUserService;
import com.mysql.cj.util.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;

@RestController
@RequiredArgsConstructor
@RequestMapping("/oapi/ding/oa")
public class DingTalkSuccessCallbackController {

    private final SystemUserService systemUserService;
    private final BiUserApprovalChangeService biUserApprovalChangeService;

//    @RequestMapping("/callback")
//    public void oaCallback(@RequestBody Map<String, Object> request) {
//
//        System.out.println(request);
//    }

    @RequestMapping("/callback")
    public String oaCallback(@RequestBody OAUserCallbackRequest request) {
        String ids = request.getIds().replaceAll("，", ",")
                .replaceAll("\n", ",")
                .replaceAll(" ", "")
                .replaceAll("；", ",")
                ;
        SystemUserPO teamLeader = systemUserService.getById(request.getUserId1());
        SystemUserPO supervisor = systemUserService.getById(request.getUserId2());
        SystemUserPO marketingManager = systemUserService.getById(request.getUserId3());
        SystemUserPO productManager = systemUserService.getById(request.getUserId4());
        SystemUserPO setStoreManager = systemUserService.getById(request.getShopsUserId());
        ArrayList<BiUserApprovalChangePO> polist = new ArrayList<>();
        for (String id : ids.split(",")) {
            if (StringUtils.isNullOrEmpty(id)){
                continue;
            }
            BiUserApprovalChangePO po = new BiUserApprovalChangePO();
            po.setStoreManagerName(setStoreManager.getUsername());
            po.setStoreManagerId(setStoreManager.getJobNumber());
            po.setTeamLeaderName(teamLeader.getUsername());
            po.setTeamLeaderId(teamLeader.getJobNumber());
            po.setSupervisorName(supervisor.getUsername());
            po.setSupervisorId(supervisor.getJobNumber());
            po.setMarketingManagerName(marketingManager.getUsername());
            po.setMarketingManagerId(marketingManager.getJobNumber());
            po.setProductManagerName(productManager.getUsername());
            po.setProductManagerId(productManager.getJobNumber());
            po.setStoreCode(request.getShops());
            po.setProductLink(id);
            po.setValidStartDate(new Date(request.getDateTime()));
            po.setUpdateTime(new Date());
            po.setCreateTime(new Date());
            po.setCreateBy("1");
            po.setUpdateBy("1");
            polist.add(po);
        }
        biUserApprovalChangeService.saveBatch(polist);
        return "success";
    }


}
